package com.baidu.foodrecipe.ui.fragment.recipes

import android.app.Dialog
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import com.baidu.foodrecipe.R
import com.baidu.foodrecipe.base.BaseFragment
import com.baidu.foodrecipe.databinding.FragmentSearchTypeBinding
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.google.android.material.chip.Chip


class SearchTypeFragment : BottomSheetDialogFragment() {
    private val viewModel: MainViewModel by activityViewModels()

    private lateinit var binding: FragmentSearchTypeBinding
    private var mealType: String = "main course"
    private var dietType: String = "Gluten Free"

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = BottomSheetDialog(requireContext())
        binding = FragmentSearchTypeBinding.inflate(layoutInflater)
        dialog.setContentView(binding.root)
        initUI()
        return dialog
    }

    private fun initUI(){
        binding.mealTypeGroup.setOnCheckedStateChangeListener { group, checkedIds ->
            //获取选中的chip的id
            val checkedId = checkedIds[0]
            //根据id获取chip
            val chip = group.findViewById<Chip>(checkedId)
            //获取chip的文本
            mealType = chip.text.toString().lowercase()
        }

        binding.dietTypeGroup.setOnCheckedStateChangeListener { group, checkedIds ->
            //获取选中的chip的id
            val checkedId = checkedIds[0]
            //根据id获取chip
            val chip = group.findViewById<Chip>(checkedId)
            //获取chip的文本
            dietType = chip.text.toString().lowercase()
        }

        binding.applyBtn.setOnClickListener {
            viewModel.search(mealType, dietType)
            dismiss()
        }

    }
}